package com.smedi.ismedi.personnel.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smedi.ismedi.database.starter.annotation.SkipSqlPermission;
import com.smedi.ismedi.personnel.core.domain.aggregatemodel.ProfileImage;
import com.smedi.ismedi.personnel.core.domain.repository.ProfileImageRepository;
import com.smedi.ismedi.personnel.core.port.adapter.persistence.model.ProfileImageEntity;
import com.smedi.ismedi.personnel.core.port.adapter.persistence.mybatis.mapper.ProfileImageMapper;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import java.util.Collections;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-6-9 19:49
 */
@Slf4j
@Service
@AllArgsConstructor
public class ProfileImageRepositoryImpl extends ServiceImpl<ProfileImageMapper, ProfileImageEntity> implements ProfileImageRepository, IService<ProfileImageEntity> {
    private final DozerUtil dozerUtil;

    @Override
    public ProfileImage selectProfileImageByImageUUID(String imageUUID) {
        QueryWrapper<ProfileImageEntity> wrapper = new QueryWrapper();
        wrapper.eq("uuid", imageUUID);
        ProfileImageEntity profileImageEntity = this.getBaseMapper().selectOne(wrapper);
        if (profileImageEntity != null) {
            return dozerUtil.convertor(profileImageEntity, ProfileImage.class);
        } else {
            return null;
        }
    }

    @Override
    public ProfileImage selectProfileImageByPersonId(Long personId) {
        QueryWrapper<ProfileImageEntity> wrapper = new QueryWrapper();
        wrapper.eq("person_id", personId);
        ProfileImageEntity profileImageEntity = this.getBaseMapper().selectOne(wrapper);
        if (profileImageEntity != null) {
            return dozerUtil.convertor(profileImageEntity, ProfileImage.class);
        } else {
            return null;
        }
    }

    @Override
    @SkipSqlPermission
    public List<ProfileImage> selectProfileImageByPersonIds(List<Long> personIds) {
        LambdaQueryWrapper<ProfileImageEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ProfileImageEntity::getPersonId, personIds);
        List<ProfileImageEntity> profileImageEntities = this.getBaseMapper().selectList(queryWrapper);
        if(!profileImageEntities.isEmpty()){
            return dozerUtil.convertor(profileImageEntities, ProfileImage.class);
        }
        return Collections.emptyList();
    }

    @Override
    public ProfileImage doInsert(ProfileImage data) {
        ProfileImageEntity profileImageEntity = dozerUtil.convertor(data, ProfileImageEntity.class);
        this.getBaseMapper().insert(profileImageEntity);
        return dozerUtil.convertor(profileImageEntity, ProfileImage.class);
    }

    @Override
    public ProfileImage doUpdate(ProfileImage data) {
        ProfileImageEntity profileImageEntity = dozerUtil.convertor(data, ProfileImageEntity.class);
        this.getBaseMapper().updateById(profileImageEntity);
        return dozerUtil.convertor(profileImageEntity, ProfileImage.class);
    }
}
